<?php
include_once '../samepart/header.php';
?>
<div class="container" style="background-color: #f5f5f5;">
    <div class="row" style="margin-left: 40px">
        <h3>订单预览<small>温馨提示：请添加你要邮递到的地址</small></h3>
    </div>
    <form id="fromOrder" action="" method="post" onsubmit="return refBack()">
        <div class="row" style="margin-top: 40px; height:60%;">
            <input type="hidden" name="t" value="1">
            <div class="col-md-10 col-md-offset-1">
                <table id="cart_table" class="table table-bordered table-striped table-hover">
                    <tr>
                        <td colspan="5">
                            <h5>收货地址</h5>
                            <select name="aid" style="width:60%" class="form-control" id="tb_adlist">

                            </select>
                            <a href="../order/self_info.php">添加收货地址</a>
                        </td>
                    </tr>
                </table>
            </div>

        </div>
        <hr>
        <div class="row">
            <div style="margin-left: 40px;">
                <h4>商品金额总计：<span id="total" class="text-danger"><b id="maxprice">￥&nbsp;&nbsp;</b></span></h4>
            </div>
        </div>
        <div class="row pull-right" style="margin-right: 40px;">
            <div style="margin-bottom: 20px;">
                <button id="btn_add" class="btn  btn-danger btn-lg" type="submit">提交订单</button>
            </div>
        </div>
    </form>
</div>
<!-- 底部 -->
<?php include_once '../samepart/footer.php' ?>

<script type="text/javascript">
    var uaid = '';

    function refBack() {
        if (uaid != '') {
            return true;
        }
        return false;
    }

    function getUserId() {
        var uid = '';
        $.ajax({
            async: false,//取消异步执行，为同步执行。进而被其他方法调用
            url: "http://localhost:80/phalapi/public/index.php?s=App.User.Login",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                'username': '<?php echo $_SESSION["username"] ?>',
                "password": '<?php echo $_SESSION["password"] ?>'
            }),
            dataType: "JSON",
            success: function (result) {
                uid = result['data']['userid'];

                // console.log("uid:"+uid)
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
        return uid;
    }

    function getGoodInfoByGid(good_id) {
        var goodname = ''
        $.ajax({
            async: false,//取消异步执行，为同步执行。进而被其他方法调用
            url: "http://localhost:80/phalapi/public/index.php?s=App.Goods.getGoodInfo",
            type: "GET",
            data: {
                "good_id": good_id
            },
            dataType: "JSON",
            success: function (result) {
                goodname = result.data.info.name;
                // console.log("data:" + username_data)
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
        return goodname;
    }

    /**
     * 用户地址获取信息获取，设置显示地址信息
     */
    $(function () {
        $.ajax({
            url: "http://localhost:80/phalapi/public/index.php?s=App.UserAddress.GetUserAddressByUid",
            type: "GET",
            data: {
                "user_id": getUserId()
            },
            dataType: "JSON",
            success: function (result) {
                var user_data = result.data.info;
                if (user_data.length > 0) {
                    // console.log('adname:'+user_data);
                    for (i = 0; i < user_data.length; i++) {
                        var name = user_data[i]['name'];
                        var phone = user_data[i]['phone'];
                        var detail = user_data[i]['detail'];
                        uaid = user_data[i]['id'];
                        var obj = document.getElementById("tb_adlist");
                        obj.options.add(new Option(name + '  ' + phone + '  ' + detail));
                    }
                } else {
                    alert("您的收货地址栏为空，建议您先添加地址")
                    // window.location.replace("../order/self_info.php");
                }
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
    })

    $(function () {
        $.ajax({
            url: "http://localhost:80/phalapi/public/index.php?s=App.CartDetail.getCartDetailInfoByCid",
            type: "GET",
            data: {
                "cart_id": '<?php echo $_GET["show_cart_id"] ?>' //uid获得当前页面的id
            },
            dataType: "JSON",
            success: function (result) {
                var cart_data = result.data.info;
                var s = "";
                var a = "";
                var maxprice = 0;
                // console.log("data:" + cart_data.length)
                for (var i = 0; i < cart_data.length; i++) {
                    j = i + 1
                    $goodname_id = result.data.info[i].gid;
                    var money = result.data.info[i].money;
                    var num = result.data.info[i].num;
                    maxprice += (num * money);
                    a += "<tr><th>" + j + "</th>" +
                        "<th>" + getGoodInfoByGid($goodname_id) + "</th>" +
                        "<th>" + money + "</th>" +
                        "<th>" + num + "</th>" +
                        "<th>" + (money * num) + "</th></tr>"
                    // s += "<tr><td>"+(n++)+"</td><td>"+gid+"</td><td>"+money+"</td><td>"+num+"</td></tr>";
                }

                s += "<tr><th>" + "序号" + "</th>" + "<th>" + "商品名称" + "</th>" +
                    "<th>" + "价格" + "</th>" + "<th>" + "数量" + "</th>" +
                    "<th>" + "小计" + "</th></tr>" + a

                $("#cart_table").append(s);
                if (maxprice == 0) {
                    $("#fromOrder").attr("action", "#")
                } else {
                    $("#fromOrder").attr("action", "../order/order.php?show_goodsprice=" + maxprice + "&show_uaid=" + uaid + "&show_uid=" + getUserId() + "&show_cid=" +<?php echo $_GET["show_cart_id"] ?>)
                }
                $("#maxprice").html(maxprice)
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
    })
</script>